/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.csv.business.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.genia.toolbox.projects.csv.bean.ConcreteFieldDescriptor;
import com.genia.toolbox.projects.csv.bean.impl.FieldDescriptorVisitorAdapter;
import com.genia.toolbox.projects.csv.business.manager.ComputeColumnNameVisitor;
import com.genia.toolbox.projects.csv.enumeration.FieldType;

/**
 * {@link com.genia.toolbox.projects.csv.bean.FieldDescriptorVisitor} that
 * compute the name of the Csv Columns.
 */
public class ComputeColumnNameVisitorImpl
    extends FieldDescriptorVisitorAdapter
    implements ComputeColumnNameVisitor
{

  /**
   * visit method for {@link ConcreteFieldDescriptor}.
   * 
   * @param field
   *          the field to visit
   * @see com.genia.toolbox.projects.csv.bean.impl.FieldDescriptorVisitorAdapter#visitConcreteField(com.genia.toolbox.projects.csv.bean.ConcreteFieldDescriptor)
   */
  @Override
  public void visitConcreteField(ConcreteFieldDescriptor field)
  {
    if (field.getFieldType() == FieldType.I18N) {
      for (Locale locale : field.getFolderDescriptor().getMappingDescriptor().getSupportedLocales()) {
        getColumnsNames().add(field.getColumnName() + " (" + locale.toString() + ")");
      }
    }
    else {
      getColumnsNames().add(field.getColumnName());
    }
  }

  /**
   * the names of the columns computed so far.
   */
  private final ThreadLocal<List<String>> columnsNames = new ThreadLocal<List<String>>();



  /**
   * clear the setted values to prevent memory leak.
   */
  public void clear()
  {
    this.columnsNames.remove();
  }



  /**
   * getter for the columnsNames property.
   * 
   * @return the columnsNames
   */
  public List<String> getColumnsNames()
  {
    List<String> result = columnsNames.get();
    if (result == null) {
      result = new ArrayList<String>();
      columnsNames.set(result);
    }
    return result;
  }
}
